package com.app.megaprosoft.modyimcarcare;

import java.sql.ResultSet;

import com.app.megaprosoft.DB.Ar;
import com.app.megaprosoft.DB.ArGroup;
import com.app.megaprosoft.DB.ArGroupMain;
import com.app.megaprosoft.DB.CarBand;
import com.app.megaprosoft.DB.CarColor;
import com.app.megaprosoft.DB.CarGeneration;
import com.app.megaprosoft.DB.CarGroup;
import com.app.megaprosoft.DB.Company;
import com.app.megaprosoft.DB.FFUser;
import com.app.megaprosoft.DB.Item;
import com.app.megaprosoft.DB.ItemGroup;
import com.app.megaprosoft.DB.ItemGroupMain;
import com.app.megaprosoft.DB.ItemPrice;
import com.app.megaprosoft.DB.ItemPromotionPrices;
import com.app.megaprosoft.DB.ItemSize;
import com.app.megaprosoft.DB.ItemStock;
import com.app.megaprosoft.DB.ItemUnit;
import com.app.megaprosoft.DB.PromotionGiveaways;
import com.app.megaprosoft.DB.Province;
import com.app.megaprosoft.DB.SQLiteDBClass;
import com.app.megaprosoft.DB.ServicePOS;
import com.app.megaprosoft.DB.UserGroup;
import com.megaprosoft.modyimcarcare.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class Carcare_Splash extends Activity {
	
	public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
	private ProgressBar progressBar;
	private int progressStatus = 0;
	private int progressStart = 0;
	private int progressMax = 22;
	
	private ImageView select;
	private TextView textView_Loading,textView_LoadingMax;
	CountDownTimer timecount;
	ResultSet rs = null;
	SQLiteDBClass myDb;
	Animation mAnimation;
	private Boolean update;
	
	//Database Service
	FFUser 					ffuser 							= new FFUser();
	UserGroup 				usergroup 						= new UserGroup();
	Ar 						ar 								= new Ar();
	ArGroupMain 			argroupmain 					= new ArGroupMain();
	ArGroup 				argroup 						= new ArGroup();
	Province 				province 						= new Province();
	CarGroup 				cargroup 						= new CarGroup();
	CarBand 				carband 						= new CarBand();
	CarGeneration 			cargeneration 					= new CarGeneration();
	CarColor 				carcolor 						= new CarColor();
	Item 					item 							= new Item();
	ItemGroup 				itemgroup 						= new ItemGroup(); 
	ItemGroupMain 			itemgroupmain 					= new ItemGroupMain();
	ItemPrice 				itemprice 						= new ItemPrice();
	ItemPromotionPrices 	itempromotionprices 			= new ItemPromotionPrices();
	PromotionGiveaways 		promotionGiveaways 				= new PromotionGiveaways();
	ItemSize 				itemsize					 	= new ItemSize();
	ItemStock 				itemstock 						= new ItemStock();
	ItemUnit 				itemunit 						= new ItemUnit();
	ServicePOS 				servicepos 						= new ServicePOS();
	
	Carcare_SessionDevice session;
	
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_carcare_activity_splash);
		//setcontext SQLite
		ffuser.setContext(getApplicationContext());
		usergroup.setContext(getApplicationContext());
		
		ar.setContext(getApplicationContext());
		argroupmain.setContext(getApplicationContext());
		argroup.setContext(getApplicationContext());
		province.setContext(getApplicationContext());
		
		cargroup.setContext(getApplicationContext());
		carband.setContext(getApplicationContext());
		cargeneration.setContext(getApplicationContext());
		carcolor.setContext(getApplicationContext());
		
		item.setContext(getApplicationContext());
		itemgroup.setContext(getApplicationContext());
		itemgroupmain.setContext(getApplicationContext());
		itemprice.setContext(getApplicationContext());
		itempromotionprices.setContext(getApplicationContext());
		promotionGiveaways.setContext(getApplicationContext());
		itemsize.setContext(getApplicationContext());
		itemstock.setContext(getApplicationContext());
		itemunit.setContext(getApplicationContext());
		
		////////////////////////////////////////////////////
		
		select =(ImageView)findViewById(R.id.Carcare_Splash_imagelogo);
		progressBar = (ProgressBar) findViewById(R.id.Carcare_Splash_progress);
		textView_Loading = (TextView) findViewById(R.id.Carcare_Splash_loading);
		textView_LoadingMax = (TextView) findViewById(R.id.Carcare_Splash_loadingMax);
		
		mAnimation = new AlphaAnimation(1, 0);
        mAnimation.setDuration(2000);
        mAnimation.setInterpolator(new LinearInterpolator());
        mAnimation.setRepeatCount(Animation.INFINITE);
        mAnimation.setRepeatMode(Animation.REVERSE); 
        
		myDb = new SQLiteDBClass(this);
		myDb.getWritableDatabase();
		
		session = new Carcare_SessionDevice(getApplicationContext());   
		if(session.isLoggedIn()){
			Intent newActivity = new Intent(Carcare_Splash.this,Carcare_Main.class);
			startActivity(newActivity);
	    	finish();
		}else{
			new  LoadingCheck().execute();
		}
		
	}
	
    public class LoadingCheck extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        select.startAnimation(mAnimation);
	        textView_Loading.setText("ทำการตรวจสอบการเชื่อมต่อ..");
	        update = false;
        }
        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
        	try{
        		Company com = new Company();
        		ResultSet rs = com.selectCompany();
        		if(rs.next()){
        			update = true;
        			ar.UpdateSQLiteArFFFromServer();
        		}
        	}catch(Exception e){
        		
        	}
//        	update = true;
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
			progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
        	if(update == true){
        		new  LoadingManageTable().execute();
        	}else{
        		textView_Loading.setText("ใช้งานโหมดออฟไลท์...");
        		select.clearAnimation();
    			Intent newActivity = new Intent(Carcare_Splash.this,Carcare_Login.class);
    			startActivity(newActivity);
    	    	finish();
        	}
			
        }
    }
	// Step 1 MangeTable
    public class LoadingManageTable extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart = 1;
	        textView_Loading.setText("ทำการตรวจสอบข้อมูล..");
	        progressStatus =1;
	        progressBar.setProgress(progressStatus);
	        textView_LoadingMax.setText(progressStart+"/"+progressMax);
        }
        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
        	myDb.Clear_table();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
			progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_Company().execute();
        }
    }
 	// Step 2 Company //สาขา
    public class Loading_Company extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart = 2;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลสาขา");
        }
        @Override
        protected Void doInBackground(String... params) {
        	usergroup.Define_FFUserGroup();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_FFUser().execute();
        }
    }
    
    // Step 3 User
    public class Loading_FFUser extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart = 3;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลผู้ใช้");
        }
        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
        	ffuser.Define_FFUser();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
        	new  Loading_ArFF().execute();
        }
    }
    
    // Step 4 _ArFF ข้อมูลลูกค้า
    public class Loading_ArFF extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart = 4;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลลูกค้า");
        }
        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
//        	ar.DEFINE_ArFF();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_ArGroupMain().execute();
        }
    }
    // Step 5 ArGroupMain กลุ่มลูกค้าหลัก
    public class Loading_ArGroupMain extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart = 5;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลกลุ่มลูกค้าหลัก");
        }
        @Override
        protected Void doInBackground(String... params) {
        	argroupmain.DEFINE_ArGroupMain();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_ArGroup().execute();
        }
    }
 // Step 6 ArGroup กลุ่มลูกค้า
    public class Loading_ArGroup extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart = 6;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลกลุ่มลูกค้า");
        }
        @Override
        protected Void doInBackground(String... params) {
        	argroup.DEFINE_ArGroup();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_Addresses().execute();
        }
    }
 // Step 7 Addresses
    public class Loading_Addresses extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart =7;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลที่อยู่ลูกค้า");
        }
        @Override
        protected Void doInBackground(String... params) {
        	province.DEFINE_Addresses();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_CarGroup().execute();
        }
    }
    // Step 8 CarGroup
    public class Loading_CarGroup extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart =8;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลกลุ่มรถ");
        }
        @Override
        protected Void doInBackground(String... params) {
        	cargroup.DEFINE_CarGroup();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_CarBands().execute();
        }
    }
 // Step 9 CarBands
    public class Loading_CarBands extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart =9;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลแบรนด์รถ");
        }
        @Override
        protected Void doInBackground(String... params) {
        	carband.DEFINE_CarBands();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_CarGenerations().execute();
        }
    }
    // Step 10 CarBands
    public class Loading_CarGenerations extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart =10;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลรุ่นรถ");
        }
        @Override
        protected Void doInBackground(String... params) {
        	cargeneration.DEFINE_CarGenerations();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_Colors().execute();
        }
    }
    
    // Step 11 Colors
    public class Loading_Colors extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart =11;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลสีรถ");
        }
        @Override
        protected Void doInBackground(String... params) {
        	carcolor.DEFINE_CarColor();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_Item().execute();
        }
    }
    // Step 12 Item
    public class Loading_Item extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart =12;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลสินค้า");
        }
        @Override
        protected Void doInBackground(String... params) {
        	item.DEFINE_Item();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_ItemGroup().execute();
        }
    }
    // Step 13 ItemGroup
    public class Loading_ItemGroup extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart =13;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลกลุ่มสินค้า");
        }
        @Override
        protected Void doInBackground(String... params) {
        	itemgroup.DEFINE_ItemGroup();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_ItemGroupMain().execute();
        }
    }
    // Step 14 ItemGroupMain
    public class Loading_ItemGroupMain extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart =14;
	        textView_Loading.setText("กำลังโหลด..กลุ่มสินค้าหลัก");
        }
        @Override
        protected Void doInBackground(String... params) {
        	itemgroupmain.DEFINE_ItemGroupMain();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_ItemPrice().execute();
        }
    }
    
    // Step 15 ItemPrice
    public class Loading_ItemPrice extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart =15;
	        textView_Loading.setText("กำลังโหลด..ราคาสินค้า");
        }
        @Override
        protected Void doInBackground(String... params) {
        	itemprice.DEFINE_ItemPrice();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_ItemPromotionPrices().execute();
        }
    }
    
    // Step 16 ItemPromotionPrices
    public class Loading_ItemPromotionPrices extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart =16;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลโปรโมชันสินค้า");
        }
        @Override
        protected Void doInBackground(String... params) {
        	itempromotionprices.DEFINE_ItemPromotionPrices();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_ItemSize().execute();
        }
    }
    
 // Step 17 ItemSize
    public class Loading_ItemSize extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart =17;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลขนาดสินค้า");
        }
        @Override
        protected Void doInBackground(String... params) {
        	itemsize.DEFINE_ItemSize();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_ItemStock().execute();
        }
    }
    
 // Step 18 ItemStock
    public class Loading_ItemStock extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart =18;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลสินค้าในคลัง");
        }
        @Override
        protected Void doInBackground(String... params) {
        	itemstock.DEFINE_ItemStock();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_ItemUnit().execute();
        }
    }
    
    // Step 19 ItemUnit
    public class Loading_ItemUnit extends AsyncTask<String, Void, Void> {
		protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart =19;
	        textView_Loading.setText("กำลังโหลด..ข้อมูลหน่วยสินค้า");
        }
        @Override
        protected Void doInBackground(String... params) {
        	itemunit.DEFINE_ItemUnit();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
			new  Loading_PromotionGiveAway().execute();
        }
    }
    
    // Step 20 PromotionGiveAway
    public class Loading_PromotionGiveAway extends AsyncTask<String, Void, Void> {
    	protected void onPreExecute() {
        	super.onPreExecute();
	        progressStart =20;
	        textView_Loading.setText("ทำการโหลด..โปรโมชั่นสินค้า");
        }
        @Override
        protected Void doInBackground(String... params) {
        	promotionGiveaways.DEFINE_PromotionGiveAway();
        	promotionGiveaways.DEFINE_PromotionGiveAwayDetail();
    		return null;
        }
		protected void onPostExecute(Void unused) {
			textView_LoadingMax.setText(progressStart+"/"+progressMax);
        	progressStatus=Integer.valueOf(progressStart*100/progressMax);
        	progressBar.setProgress(progressStatus);
        	select.clearAnimation();
			Intent newActivity = new Intent(Carcare_Splash.this,Carcare_Login.class);
			startActivity(newActivity);
	    	finish();
        }
    }
    
	@Override
	public void onBackPressed() {
	  
	        new AlertDialog.Builder(this)
	        .setTitle("ออกจากโปรแกรม")
	        .setMessage("ต้องการออกจากโปรแกรม")
	                .setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {

	                    public void onClick(DialogInterface arg0, int arg1) {
	                        finish();
	                        System.exit(0);
	                    }
	                })
	                .setPositiveButton(android.R.string.no, null).create().show();
	} 
	
	
}
